package com.jshoperxms.components.scheduler;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.entity.YdStatusInfoT;
import com.jshoperxms.entity.YdWorldStatusInfoT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdRecordRdTService;
import com.jshoperxms.service.YdStatusInfoTSerivce;
import com.jshoperxms.service.YdWorldStatusInfoTService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by sdywcd on 2019/1/4.
 * Des:
 */
@Component
public class YdWorldScheduler {
    private static final org.slf4j.Logger LOGGER =  LoggerFactory.getLogger(YdWorldScheduler.class);

    @Resource
    private YdWorldStatusInfoTService ydWorldStatusInfoTService;

    @Resource
    private YdRecordRdTService ydRecordRdTService;

    @Resource
    private YdStatusInfoTSerivce ydStatusInfoTSerivce;

    @Resource
    private MemberTService memberTService;
    /**
     * 每日计算朗读乐园中所有的树叶能够生成多少树木，和相关数据总数据信息并保存
     */
    @Scheduled(cron = "0 0 0 * * ?")//每天0点执行一次结算计算0 0 0 * * ?
    public void calYdWorldInfo(){
        LOGGER.info("开始执行每日树叶生成树木定时任务");
        //计算世界上的总表扬，通过yd_record表
        int totalworldbiaoyang=0;
        int totalworldyuedu=0;
        int totalworldleafs=0;
        int totalworldshare=0;
        int totalworldtrees=0;
        String sqlforbiaoyang="select sum(totalbiaoyang) as totalworldbiaoyang,sum(totalyuedu) as totalworldyuedu,sum(totalleafs) as totalworldleafs,sum(totalshare) as totalworldshare from YdStatusInfoT";
        List<YdStatusInfoT>list=this.ydStatusInfoTSerivce.findByHql(sqlforbiaoyang);
        //List<YdWorldStatusInfoT> biaoyanglist=this.ydWorldStatusInfoTService.queryBySql(YdWorldStatusInfoT.class,sqlforbiaoyang);
        Iterator item = list.iterator();
        while(item.hasNext()){
            Object[] objects = (Object[])item.next();
            totalworldbiaoyang=Integer.parseInt(objects[0]+"");
            totalworldyuedu=Integer.parseInt(objects[1]+"");
            totalworldleafs=Integer.parseInt(objects[2]+"");
            totalworldshare=Integer.parseInt(objects[3]+"");
            totalworldtrees=totalworldleafs/1000;
        }
        int totalworldmembers=this.memberTService.count(MemberT.class).intValue();
        LOGGER.info("世界表扬数量="+totalworldbiaoyang);
        LOGGER.info("世界阅读数量="+totalworldyuedu);
        LOGGER.info("世界树叶数量="+totalworldleafs);
        LOGGER.info("世界分享数量="+totalworldshare);
        LOGGER.info("世界树木数量="+totalworldtrees);
        LOGGER.info("世界人口数量="+totalworldmembers);

        //计算当日的sunshine
        long currenttime=(new Date().getTime()/1000)-172800;
        Criterion criterion=Restrictions.lt(TagWords.CREATETIME,currenttime);
        int twodaysrdcount=this.ydRecordRdTService.count(YdRecordRdT.class,criterion).intValue();
        int sunshine=(totalworldyuedu*totalworldtrees/totalworldmembers)+twodaysrdcount;
        LOGGER.info("世界阳光数量="+sunshine);
        YdWorldStatusInfoT bean=new YdWorldStatusInfoT();
        bean.setId(PrimaryKey.get().toString());
        bean.setTotalworldbiaoyang(totalworldbiaoyang);
        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
        bean.setCreatetime(DateTools.getSystemTimeLong());
        bean.setCreatorid("0");
        bean.setUpdatetime(DateTools.getSystemTimeLong());
        bean.setVersiont(1);
        bean.setTotalworldyuedu(totalworldyuedu);
        bean.setTotalworldleafs(totalworldleafs);
        bean.setTotalworldshare(totalworldshare);
        bean.setTotalworldtrees(totalworldtrees);
        bean.setTotalworldmembers(totalworldmembers);
        bean.setSunshine(sunshine);
        this.ydWorldStatusInfoTService.saveOrUpdate(bean);
    }
}
